package com.ghost.alibabaghost.web.controller.projects;

import com.alibaba.fastjson.JSONObject;
import com.ghost.alibabaghost.api.model.projects.ProjectModel;
import com.ghost.alibabaghost.api.service.projects.ProjectService;
import com.ghost.alibabaghost.result.QueryPageInfo;
import com.ghost.alibabaghost.result.SingleResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.UUID;

/**
 * @author yaolisheng
 * @version 1.0
 * @date 2022/1/18 12:24
 */
@RestController
@RequestMapping("project")
public class ProjectController {

    private static final Logger logger = LoggerFactory.getLogger(ProjectController.class);
    private static final String HORIZONTAL_ = "-";

    @Autowired
    private ProjectService projectService;

    /**
     * 获取项目列表
     *
     * @param queryPageInfo
     * @return
     */
    @RequestMapping("getProjectList")
    public SingleResult<QueryPageInfo<ProjectModel>> getProjectList(QueryPageInfo<ProjectModel> queryPageInfo) {
        logger.info("getProjectList（获取项目列表）->params:" + queryPageInfo.toString());
        try {
            ProjectModel projectModel = JSONObject.parseObject(queryPageInfo.getData(), ProjectModel.class);
            List<ProjectModel> projectModels = projectService.querySelective(projectModel);
            queryPageInfo.pageHelper(projectModels);
            logger.info("getProjectList（获取项目列表）->response:" + queryPageInfo);
            return SingleResult.buildSuccess(queryPageInfo);
        } catch (Exception e) {
            logger.error("getProjectList（获取项目列表）->", e);
            return SingleResult.buildFailure();
        }
    }

    /**
     * 项目新增
     *
     * @param projectModel
     * @return
     */
    @RequestMapping("insertPeoject")
    public SingleResult<String> insertPeoject(@RequestBody ProjectModel projectModel) {
        logger.info("insertPeoject（项目新增）->params:" + projectModel);
        try {
            projectModel.setProjectId(UUID.randomUUID().toString().replaceAll(HORIZONTAL_,""));
            projectModel.setProjectStatus("0");
            projectModel.setObjectNum(0);
            projectService.insertSelective(projectModel);
            return SingleResult.buildSuccess("新增成功");
        } catch (Exception e) {
            logger.error("insertPeoject（项目新增）->" + e);
            return SingleResult.buildFailure();
        }
    }
}
